package com.project.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dtyunxi.rest.RestResponse;
import com.project.common.Constants;
import com.project.entity.user.User;

public class LoginUserUtil {
	
	private final static Logger logger = LoggerFactory.getLogger(LoginUserUtil.class);
	
	public static User getLoginUser(HttpServletRequest request,HttpServletResponse response){
		logger.info("-----------------获取登录用户信息.requestMethod:{}----------------",request.getMethod());
		//只要是带自定义header的跨域请求，在发送真实请求前都会先发送OPTIONS请求，
		//浏览器根据OPTIONS请求返回的结果来决定是否继续发送真实的请求进行跨域资源访问。所以复杂请求肯定会两次请求服务端。
		if(!"OPTIONS".equalsIgnoreCase(request.getMethod())){
			String auth = request.getHeader("auth");
			if(StringUtils.isNotBlank(auth)){
				logger.info("-----------------获取登录用户信息.auth:{}----------------",auth);
				User user = (User) EhcacheUtil.get(Constants.LOGIN_USER_CACHE, auth);
				return user;
			}
		}
		return null;
		
	}

}
